package com.atguigu.Flink.wordCount;

import com.atguigu.Flink.POJO.WordCount;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

public class Flink01_test {
    public static void main(String[] args) throws Exception {
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
//        env.setParallelism(1);
//        //DataStreamSource<String> ds = env.socketTextStream("hadoop102", 8888);
//       DataStreamSource<String> ds = env.readTextFile("Input/words.txt");
//        SingleOutputStreamOperator<WordCount> flatMap = ds.flatMap(new FlatMapFunction<String, WordCount>() {
//            @Override
//            public void flatMap(String s, Collector<WordCount> collector) throws Exception {
//                String[] s1 = s.split(" ");
//                for (String s2 : s1) {
//                    collector.collect(new WordCount(s2, 1L));
//                }
//            }
//        });
//        SingleOutputStreamOperator<WordCount> sum = flatMap.keyBy(new KeySelector<WordCount, String>() {
//            @Override
//            public String getKey(WordCount wordCount) throws Exception {
//                return wordCount.getWord();
//            }
//        }).sum("count");
//        sum.print();
//        env.execute();
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
//        env.setParallelism(1);
//        DataStreamSource<String> ds = env.readTextFile("Input/words.txt");
//        SingleOutputStreamOperator<Tuple2<String, Long>> flatmap = ds.flatMap(new FlatMapFunction<String, Tuple2<String, Long>>() {
//            @Override
//            public void flatMap(String s, Collector<Tuple2<String, Long>> collector) throws Exception {
//                String[] s1 = s.split(" ");
//                for (String s2 : s1) {
//                    collector.collect(Tuple2.of(s2, 1L));
//                }
//            }
//        });
//        KeyedStream<Tuple2<String, Long>, String> keyby = flatmap.keyBy(new KeySelector<Tuple2<String, Long>, String>() {
//            @Override
//            public String getKey(Tuple2<String, Long> stringLongTuple2) throws Exception {
//                return stringLongTuple2.f0;
//            }
//        });
//        SingleOutputStreamOperator<Tuple2<String, Long>> sum = keyby.sum(1);
//        sum.print();
//        env.execute();
    }
}
